import 'package:contacts_service/contacts_service.dart';
import 'package:dmz/base/controller/base_controller.dart';
import 'package:dmz/business/home/model/moto_model.dart';
import 'package:dmz/business/home/widgets/shanre_details_widgets.dart';
import 'package:dmz/request/apis.dart';
import 'package:dmz/request/request_client.dart';
import 'package:dmz/routers/routers.dart';
import 'package:dmz/utils/customize_loding.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import 'share_moto_controller.dart';

class ShareDetailsController extends BaseController {
  String phone = '';
  MotoModel model = MotoModel();
  bool selectFirst = true;
  bool selectDay = false;
  TextEditingController inputController = TextEditingController();
  int shareTime = -1;
  Contact? contact;

  @override
  void loadNet() {}

  @override
  void onHidden() {}

  @override
  void onReady() {
    super.onReady();
    phone = Get.find<ShareMotoController>().phone;
    model = Get.find<ShareMotoController>().model;
    contact = Get.find<ShareMotoController>().selectData;
    if (contact != null) phone = contact!.phones!.first.value!;
    update();
  }

  ///时效选择
  showSelectWindow() {
    EMILoding.showBottomWindow(
        ShareDetailsWidgets.shareSelectWidget((firstSelect) {
          debugPrint('时间选择1 -- ${firstSelect}');
          selectFirst = firstSelect;
          shareTime = -1;
          update(['select']);
        }, (dataSelect) {
          debugPrint('时间选择2 -- ${dataSelect}');
          selectDay = dataSelect;
          update(['select']);
        }, () {
          //时效选择
          if (selectDay) {
            EMILoding.hitLoding('设置完整的限时时间'.tr);
            return;
          }
          Get.back();
          update(['select']);
          didShareMoto();
        }), close: () {
      update(['select']);
    });
  }

  ///分享车辆
  didShareMoto() {
    getShareTiem();
    RequestClient().post<Map>(APIS.didShare, data: {
      'ebike_id': model.id,
      'phone': phone,
      'share_time': shareTime
    }).then((value) {
      EMILoding.hitLoding('分享成功'.tr, isScuess: true);
      Get.find<ShareMotoController>().getShareList();
      //返回分享主页面
      Get.until((route) => route.settings.name == RouteGet.shareKey);
    });
  }

  ///分享时间
  getShareTiem() {
    if (!selectFirst) {
      shareTime = selectDay
          ? int.parse(inputController.text) * 60 * 60 * 24
          : int.parse(inputController.text) * 60 * 60;
    }
  }
}

class ShareDetailsBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut(() => ShareDetailsController());
  }
}
